import {
	pub,
	sub
} from '@/common/event.js'
import store from '@/store/index.js'
import {
	getRqst
} from '@/common/request.js'

export default {
	namespaced: true,
	state: {
		messages: [],
		status: {},
		friends: {},
		currentFriendId: ''
	},
	mutations: {
		clearAll:(state,payload)=>{
			state.messages = []
			state.status = {}
			state.friends = {}
			state.currentFriendId = ''
		},
		setCurrentFriendId: (state, payload) => {
			console.log(111222,payload)
			state.currentFriendId = payload
		},
		setStatusLoading: (state, payload) => {
			state.status[payload.id].loading = true
		},
		setMoreMessageState: (state, payload) => {
			state.messages = state.messages.concat(payload.data)
			if (payload.data.length < 20) {
				state.status[payload.id].loading = false
				state.status[payload.id].finish = true
			} else {
				state.status[payload.id].loading = false
				state.status[payload.id].finish = false
			}
		},
		addMessage: (state, payload) => {
			console.log("0003", state.status)
			console.log("0004", payload)
			const friend_id = payload.friend_id == store.state.user.user.id ? payload.user_id : payload.friend_id
			if (state.status[friend_id]) {
				state.messages.unshift(payload)
				pub("vuex_fchat_addMessage", {})
			} else {
				store.dispatch({
					type: "fchat/getFChatMessageInit",
					payload: {
						id: friend_id,
						callback: function() {
							pub("vuex_fchat_addMessage", {})
						}
					}
				})
			}

		},
		setFriendChatMessage: (state, payload) => {

			let finish = false
			if (payload.data.data.length < 20) {
				finish = true
			}
			let loading = false
			state.messages = state.messages.concat(payload.data.data)
			state.status[payload.data.friend.id] = {
				finish,
				loading
			}
			console.log("0002", state.status)
			state.friends[payload.data.friend.id] = payload.data.friend
			state.currentFriendId = payload.data.friend.id
		}
	},
	actions: {
		async getFChatMessageInit({
			state,
			commit
		}, {
			payload
		}) {
			if (state.status[payload.id]) {
				console.log("0001", state.status,payload)
				commit('setCurrentFriendId', payload.id)
				return payload.callback()
			}
			const id = store.state.user.user.id
			const ret = await getRqst().get(`/chat/chat/fchat/${id}/${payload.id}`)
			if (ret.success) {
				commit('setFriendChatMessage', {
					id: payload.id,
					data: ret.data
				})
				payload.callback()
			}
		},
		async getMoreMessage({
			state,
			commit
		}, {
			payload
		}) {
			
			const id = store.state.user.user.id
			const ret = await getRqst().get(`/chat/chat/message/${id}/${payload.id}`, {
				currentTime: payload.time,
				currentId: payload.currentId
			})
			if (ret.success) {
				commit('setMoreMessageState', {
					data: ret.data,
					id: payload.id
				})
				payload.callback()
			}
		},
	},
	init: function() {
		console.log(`执行fchat初始化`)
		sub("socket_fmessage", function(data) {
			store.commit("fchat/addMessage", data)
		})
		sub("logout",function(){
			store.commit("fchat/clearAll", {})
		})
	}
}